import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 150
    Component.onCompleted: {
        cmbChannel.currentIndex = mymodbus.encoderChannel;
        cmbFiltering.currentIndex = mymodbus.encoderFiltering;
        cmbDirection.currentIndex = mymodbus.encoderDirection
        interval.text = mymodbus.encoderInterval
    }
    Timer {
        id:funcTimer
                running: false
                repeat: false
                interval:100
                onTriggered: {
                    savePara()
                }
            }
    function savePara()
    {
        var res = mymodbus.slotSaveEncoderPara(cmbChannel.currentIndex, cmbDirection.currentIndex, cmbFiltering.currentIndex, Number(interval.text));

        if(res)
        {
            //comSettingSaveOK=true;
            saveOKDialog.title=qsTr("保存成功")
            saveOKDialog.accepted.connect(function() {
                saveOKDialog.close()

            })
        }
        else
        {
            saveOKDialog.title=qsTr("保存失败")
            saveOKDialog.accepted.connect(function() {
                saveOKDialog.close()
            })
        }
        loaderText.visible= false;

        saveOKDialog.open();
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: generalParaLoader
        visible:false
        z:101
    }
    Dialog {
        anchors.centerIn: parent
        id: saveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        title:qsTr("保存成功！")
        modal: true
        closePolicy: "CloseOnEscape"
        //onAccepted: saveOKDialog.close()
        z:100
        //onRejected: console.log("Cancel clicked")
    }
    Rectangle{
        id: gsMainRec
        anchors.fill: parent
        color:"transparent"
        Rectangle
        {
            color:"transparent"
            width: 800
            height: 280
            GridLayout{
                id: generalSettingsLayout
                rows: 2
                columns: 4
                anchors.centerIn: parent
                rowSpacing: 40
                Button
                {
                    Layout.row:0
                    Layout.column:0
                    Layout.preferredWidth: btnWidth
                    Layout.preferredHeight: btnHeight
                    // x: 152
                    // y: 13
                    text: qsTr("通道")
                    font.pixelSize: 18
                    background: Rectangle{
                        color: btnColor
                            }
                    onClicked: {
                    }
                }
                MyComboBox
                {
                    id:cmbChannel
                    Layout.row:0
                    Layout.column:1
                    Layout.preferredWidth: btnWidth
                    Layout.preferredHeight: btnHeight
                    borderColor:"white"
                    fillColor:"#0A3653"
                    model:[1,2,3]
                    currentIndex: mymodbus.encoderChannel
                }
                    Button
                    {
                        Layout.row:0
                        Layout.column:2
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // x: 152
                        // y: 13
                        text: qsTr("滤波")
                        font.pixelSize: 18
                        background: Rectangle{
                            color: btnColor
                                }
                        onClicked: {
                        }
                    }
                    MyComboBox
                    {
                        id:cmbFiltering
                        Layout.row:0
                        Layout.column:3
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        borderColor:"white"
                        fillColor:"#0A3653"
                        model:[0,1,2,3,4,5]
                        currentIndex: mymodbus.encoderFiltering
                    }
                    Button
                    {
                        Layout.row:1
                        Layout.column:0
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // x: 152
                        // y: 13
                        text: qsTr("计数方向")
                        font.pixelSize: 18
                        background: Rectangle{
                            color: btnColor
                                }
                        onClicked: {
                            cmbPowerAutoZero.popup.open()
                            //miniKeyBoard.lineValue = "";
                            //miniKeyBoard.textInput=oldPwd;
                        }
                    }

                    MyComboBox
                    {
                        id:cmbDirection
                        Layout.row:1
                        Layout.column:1
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        borderColor:"white"
                        fillColor:"#0A3653"
                        model:[qsTr("正向"), qsTr("反向")]
                        currentIndex:mymodbus.encoderDirection
                    }
                    Button
                                        {
                                            Layout.row:1
                                            Layout.column:2
                                            Layout.preferredWidth: btnWidth
                                            Layout.preferredHeight: btnHeight
                                            // x: 152
                                            // y: 13
                                            text: qsTr("间隔")
                                            font.pixelSize: 18
                                            background: Rectangle{
                                                color: btnColor
                                                    }
                                            onClicked: {
                                                enterNumberPanel.lastButtonIndex = 4;
                                                enterNumberPanel.lastViewIndex =14;
                                                enterNumberPanel._isNumber=true;
                                                enterNumberPanel._isDecimal=false;
                                                enterNumberPanel.itemName=qsTr("间隔")
                                                enterNumberPanel.itemValue=interval.text
                                                enterNumberPanel.textInput=interval;                                       enterNumberPanel.lastViewUrl="NormalSettings.qml"
                                                enterNumberPanel.visible=true;
                                                enterNumberPanel.lastRec=gsMainRec
                                                gsMainRec.visible = false
                                            }
                                        }

                                        Rectangle {
                                            id: recInterval
                                            width:300
                                            height:30
                                            // x: 218
                                            // y: 7
                                            color: "transparent"
                                            border.width: 0
                                            border.color: "white"
                                            Layout.row:1
                                            Layout.column:3
                                            Layout.preferredWidth: btnWidth
                                            Layout.preferredHeight: btnHeight
                                            // 绘制一条边框
                                            Shape {
                                                anchors.fill: parent
                                                ShapePath {
                                                    strokeWidth: 3
                                                    strokeColor: "white"
                                                    strokeStyle: ShapePath.SolidLine
                                                    startX: 0
                                                    startY: recInterval.height
                                                    PathLine {
                                                        x: recInterval.width
                                                        y: recInterval.height
                                                    }
                                                }
                                            }
                                            TextInput{
                                                id:interval
                                                anchors.fill: parent
                                                readOnly: true
                                                //echoMode: TextInput.Password
                                                color:"white"
                                                font.pixelSize: 25
                                                horizontalAlignment: Text.AlignHCenter
                                                verticalAlignment: Text.AlignBottom
                                                text:mymodbus.encoderInterval
                                                MouseArea {
                                                       anchors.fill: parent
                                                       onClicked: {
                                                           enterNumberPanel.lastButtonIndex = 4;
                                                           enterNumberPanel.lastViewIndex =14;
                                                           enterNumberPanel._isNumber=true;
                                                           enterNumberPanel._isDecimal=false;
                                                           enterNumberPanel.itemName=qsTr("间隔")
                                                           enterNumberPanel.itemValue=interval.text
                                                           enterNumberPanel.textInput=interval;                                       enterNumberPanel.lastViewUrl="NormalSettings.qml"
                                                           enterNumberPanel.visible=true;
                                                           enterNumberPanel.lastRec=gsMainRec
                                                           gsMainRec.visible = false
                                                       }
                                                   }

                                            }
                                        }

            }

        }
        Button
        {
            id:btnAdvSettingsOK
            x: 572
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#C070B0"
                    }

            text: qsTr("确定")
            font.pixelSize: 25
            palette.buttonText: "white"
            Connections {
                target: btnAdvSettingsOK
                onClicked: {
                    if(loaderText.visible)
                    {
                        return;
                    }
                    loaderText.visible=true;
                    funcTimer.start();
                }
            }
        }
       Button
        {
            id:btnReturn
            x: 688
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#B0B0D0"
                    }

            text: qsTr("返回")
            font.pixelSize: 25
            palette.buttonText: "white"
            Connections {
                target: btnReturn
                onClicked: {
                    bottomBar.selected=4
                    myLoader.source="SettingsPanel.qml"
                }
            }
        }

    }
    EnterNumberPanel{
          id:enterNumberPanel
          width: 800
          height: 391
          visible:false
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: encoderParaLoader
        visible:false
        z:101
    }
}


